local Copy = require("copy.CopyL")
local Lang = require("common.Lang")
local Msg = require("core.Msg")
local Obj = require("core.Obj")
local TaskLogic = require("task.TaskLogic")
local TaskDefine = require("task.Define")
local Guide = require("task.Guide")
local TaskCallbacks = require("task.TaskCallbacks")
local Broadcast = require("broadcast.Broadcast")
local BountyLogic = require("task.BountyLogic")
local BeginStory = require("scene.BeginStory")
local ObjHuman = require("core.ObjHuman")
local TaskHelp = require("task.TaskHelp")
local ItemDefine = require("bag.ItemDefine")
local Map = require("core.Map")
local MapExcel = require("excel.Map")
local FamilyBountyLogic = require("task.FamilyBountyLogic")
local map_mgr = _G.map_mgr
local Status = require("scene.Status")
local TaskGroup = require("task.TaskGroup")
local BagLogic = require("bag.BagLogic")
local MonthCardLogic = require("vip.MonthCardLogic")

function CG_TASK_ACCEPT(human,msg, auto)
  local config = TaskHelp.getTaskConfig(msg.taskID)
  if config.mainType == TaskDefine.TASK_TYPE_1_DAILY then
      return
  end
	local ret = TaskLogic.taskCanAccept(human, msg.taskID)
	if ret == TaskDefine.TASK_ACCEPT_OK then
		TaskLogic.taskDoAccept(human, msg.taskID)
		
		BeginStory.onTaskAccept(human, msg.taskID)
		
		if not auto then
		  local msgRet = Msg.gc.GC_TASK_ACCEPT
		  msgRet.taskID = msg.taskID
		  msgRet.retCode = ret
		  Msg.send(msgRet, human.fd)
		end
			
		return ret
	end
	if not auto then
	   Broadcast.sendBroadcast(human, Lang.MOUSE, TaskDefine.errAcceptTip[ret])
	end
end

function CG_TASK_FINISH(human,msg)
	local ret = TaskLogic.taskCanFinish(human, msg.taskID)
	if ret == TaskDefine.TASK_FINISH_OK then
	  BeginStory.onTaskFinish(human, msg.taskID)
		TaskLogic.taskDoFinish(human, msg.taskID)		
		return 
	end

	Broadcast.sendBroadcast(human, Lang.MOUSE, TaskDefine.errFinishTip[ret])
end

function CG_TASK_ABANDON(human,msg)
	local ret = TaskLogic.taskCanAbandon(human, msg.taskID)
	if ret == TaskDefine.TASK_ABANDON_OK then
		TaskLogic.taskDoAbandon(human, msg.taskID)
		
		local msgRet = Msg.gc.GC_TASK_ABANDON
		msgRet.taskID = msg.taskID
		msgRet.retCode = ret
		Msg.send(msgRet, human.fd)
		
		return
	end
	
	Broadcast.sendBroadcast(human, Lang.MOUSE, TaskDefine.errAbandonTip[ret])
end

function CG_TASK_GET_PLACE(human, msg)
	TaskCallbacks.taskReachPlaceCB(human, msg.taskID)
end

function CG_GUIDE_PANEL_QUERY_LIST(human,msg)
	Guide.sendGuidePanelQueryList(human,msg.index)
end

function CG_GUIDE_PANEL_FINISH(human,msg)
	human.db.guidePanel[msg.index] = true
end

function CG_BOUNTY_LIST(human)
	BountyLogic.sendBountyList(human)
end

function CG_BOUNTY_REFRESH(human)
	BountyLogic.refreshBountyList(human)
end

function CG_BOUNTY_AUTO_COMPLETE(human, msg)
	BountyLogic.autoComplete(human, msg.taskID)
end

function CG_BOUNTY_ADD_CNT(human)
  BountyLogic.addCnt(human)
end

function CG_TASK_FLY(human, msg)
	-- 死亡不能传
	if human.db.hp < 1 then		
		Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.TASK_FLY_FAIL_DIE)
		return
	end
  if not MapExcel.scene[msg.mapId] then
    return
  end
	local mapID = Map.scene_id_to_map_id[human.scene_id]
	local mapConfig = MapExcel.scene[mapID]
	if mapConfig.can_task_fly == 0 then
		Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.TASK_FLY_FAIL_MAP)
		return
	end
	if human.db.lv < 30 then --
    Broadcast.sendBroadcast(human,Lang.MOUSE, string.format(Lang.ITEM_FLY_LV_NOT_ENOUGH,30))
    return
  end
	
	-- 战斗状态无法传送
	local tempSec = ObjHuman.checkFightSec(human,20)
	if tempSec>0 then
		Broadcast.sendBroadcast(human, Lang.CENTER, string.format(Lang.TASK_FLY_FAIL_SKILL,tempSec))
		return
	end
	
	if TaskHelp.checkTaskCanFly(human, msg.mapId, msg.mX, msg.mY) == true then
    if not MonthCardLogic.isHaveSpec(human) then
  	  if BagLogic.getItemCount(human, ItemDefine.ITEM_ID_FLY) < 1 then
  				Broadcast.sendBroadcast(human, Lang.CENTER, Lang.TASK_FLY_FAIL_ITEM)
  				return
  		end
    	BagLogic.delItemsById(human, ItemDefine.ITEM_ID_FLY, 1,"task_fly")
    end
		local x
		local y
		if Map.is_same_logic(msg.mapId) then
			 x, y = map_mgr.get_fix_xy(msg.mapId, msg.mX, msg.mY)
		else
		   x, y = msg.mX, msg.mY
		end
		
		local newMapID = msg.mapId
		if not Copy.isCopy(newMapID) and newMapID== mapID then
			-- 先切状态再跳
			Status.doHumanControlStatusChange(human, Status.HUMAN_STATUS_NORMAL)
			Obj.jump(human, x, y, Msg.gc.GC_JUMP)
	  elseif Copy.isCopy(newMapID) then
	     assert(nil, newMapID)
		else
			ObjHuman.enterMap(human, newMapID, x, y)
		end
	else
		Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.TASK_FLY_FAIL_POINT)
	end
end

function CG_DAILY_AUTO_COMPLETE(human,msg)
	local msgRet = Msg.gc.GC_DAILY_AUTO_COMPLETE
	msgRet.ret,msgRet.msg = TaskGroup.taskAutoFinish(human,msg.taskID)
	Msg.send(msgRet, human.fd)
end

function CG_DAILY_ADD_CNT(human)
  TaskGroup.addCnt(human)
end

function WL_TASK_GROUP_POOL(fd, msg)
  TaskGroup.synsTaskPool(msg.data)
  print("WL_TASK_GROUP_POOL:",_G.logic_id)
end

function CG_FAMILY_BOUNTY_LIST(human)
	FamilyBountyLogic.sendFamilyBountyList(human)
	FamilyBountyLogic.sendFamilyBountyOffer(human)
end

function CG_FAMILY_BOUNTY_REFRESH(human)
	FamilyBountyLogic.doTaskRefresh(human)
end

function CG_FAMILY_BOUNTY_OFFER(human)
	FamilyBountyLogic.offerTask(human)
end

function CG_FAMILY_BOUNTY_ACCEPT(human, msg)
	FamilyBountyLogic.doTaskAccept(human, msg.offerUuid)
end